﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using dokuzeylulworkshop.BaseUtility;
using System.Collections.Generic;
using DokuzEylul.Sinav.Entities;

namespace DokuzEylul.Sinav.SinavPaneli
{
    public partial class DersKonu : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataBind();
                this.IsSave = false;
                
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("Seçiniz", "-1"));
            }
        }
        public IList<SinavDersKonu> DersKonuList
        {
            get
            {
                IList<SinavDersKonu> _result = Session["Ders_Konu_List"] as IList<SinavDersKonu>;
                if (_result == null)
                    Session["Ders_Konu_List"] = _result = m_dbmaster.DersKonuInstance.GetAllDersKonu();
                return _result;
            }
        }
        public SinavDersKonu CurrentDersKonu
        {
            get
            {
                SinavDersKonu _result = Session["Ders_Konu"] as SinavDersKonu;
                if (_result == null)
                    Session["Ders_Konu"] = _result = new SinavDersKonu();
                return _result;
            }
            set
            {
                Session["Ders_Konu"] = value;
            }
        }
        public bool IsSave
        {
            get
            {
                return (bool)ViewState["Save_Mode"];
            }
            set
            {
                ViewState["Save_Mode"] = value;
            }
        }
        protected void btnYeni_Click(object sender, EventArgs e)
        {
            this.IsSave = true;
            pnlcontent.Enabled = true;
            this.clearForm();
        }
        protected void btnKaydet_Click(object sender, EventArgs e)
        {
            CurrentDersKonu.Icerik = txtIcerik.Text;
            CurrentDersKonu.Baslik = txtBaslik.Text;
            CurrentDersKonu.Anaid = int.Parse(DropDownList1.SelectedValue.ToString());
            if (IsSave)
            {
                m_dbmaster.DersKonuInstance.SaveDersKonu(CurrentDersKonu);
                this.DersKonuList.Add(CurrentDersKonu);
                this.clearForm();
            }
            else
            {
                m_dbmaster.DersKonuInstance.UpdateDersKonu(CurrentDersKonu);
                for (int i = 0; i < DersKonuList.Count; i++)
                {
                    if (CurrentDersKonu.SinavDersKonuid == DersKonuList[i].SinavDersKonuid)
                    {
                        DersKonuList[i].Icerik = CurrentDersKonu.Icerik;
                        DersKonuList[i].Baslik  = CurrentDersKonu.Baslik;
                        DersKonuList[i].Anaid = CurrentDersKonu.Anaid;
                    }
                }
            }
            GridView1.DataBind();
            DropDownList1.DataBind();
        }
        protected void btnSil_Click(object sender, EventArgs e)
        {
            m_dbmaster.DersKonuInstance.DeleteDersKonu(CurrentDersKonu);
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            DataKey _key = GridView1.SelectedDataKey;
            this.CurrentDersKonu = m_dbmaster.DersKonuInstance.GetDersKonu((int)_key.Value);
            txtBaslik.Text = this.CurrentDersKonu.Baslik;
            txtIcerik.Text = this.CurrentDersKonu.Icerik;
            if (this.CurrentDersKonu.Anaid == 0)
                DropDownList1.SelectedValue = "-1";
            else
                DropDownList1.SelectedValue = this.CurrentDersKonu.Anaid.ToString();
            pnlcontent.Enabled = true;
            this.IsSave = false;
        }
        protected void GridView1_DataBinding(object sender, EventArgs e)
        {
            GridView1.DataSource = this.DersKonuList;
        }
        private void clearForm()
        {
            foreach (Control _ctrl in pnlcontent.Controls)
            {
                if ((_ctrl as TextBox) is TextBox)
                    (_ctrl as TextBox).Text = "";
            }
        }
        protected void DropDownList1_DataBinding(object sender, EventArgs e)
        {
            DropDownList1.DataSource = m_dbmaster.DersKonuInstance.GetAllDersKonu();
        }
    }
}
